package com.pxst.fx.controller;

import com.pxst.exception.BusinessException;
import com.pxst.fx.entity.SysFxOrderExcelModel;
import com.pxst.fx.req.SysFxOrderBatchReq;
import com.pxst.fx.req.SysFxOrderChildInfoReq;
import com.pxst.fx.req.SysFxOrderChildStatusReq;
import com.pxst.fx.req.SysFxOrderEntityReq;
import com.pxst.fx.resp.SysFxOrderEntityResp;
import com.pxst.fx.service.SysFxOrderEntityService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Settlement_下发接口
 *
 * @author hy
 * @TableName SysFxOrderEntityController
 */
@RestController
@RequestMapping("/api/fxOrder")
@Api(tags = "下发相关接口")
@Validated
public class SysFxOrderEntityController {

    @Autowired
    private SysFxOrderEntityService sysFxOrderEntityService;

    @ApiOperation("分页查询")
    @GetMapping
    public ApiResp<PageResp<SysFxOrderEntityResp>> getSysFxOrderList(SysFxOrderEntityReq sysFxOrderEntityReq) {
        return sysFxOrderEntityService.getSysFxOrderList(sysFxOrderEntityReq);
    }

    @ApiOperation("汇出报表")
    @GetMapping("/download")
    public void download(SysFxOrderEntityReq sysFxOrderEntityReq, HttpServletResponse response) {
        List<SysFxOrderExcelModel> excelModels = sysFxOrderEntityService.download(sysFxOrderEntityReq);
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            ExcelUtils.exportExcel(excelModels, "下发汇出报表", "下发报表", SysFxOrderExcelModel.class, dateFormat.format(new Date()) + "-Fx", response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("汇出报表错误");
        }
    }

    @ApiOperation("下发详情")
    @GetMapping("/{id}/{rp}")
    public ApiResp fxOrderDetail(@PathVariable Long id, @PathVariable Integer rp) {
        if (rp == null) {
            rp = 100;
        }
        return sysFxOrderEntityService.fxOrderDetail(id, rp);
    }


    @ApiOperation("批准按钮")
    @PostMapping("/{id}/batch")
    public ApiResp batch(@PathVariable Long id,SysFxOrderBatchReq sysFxOrderBatchReq) {
        return sysFxOrderEntityService.batch(id, sysFxOrderBatchReq);
    }

    @ApiOperation("拒绝按钮")
    @PutMapping("/{id}/reject")
    public ApiResp reject(@PathVariable Long id) {
        return sysFxOrderEntityService.reject(id);
    }

    @ApiOperation("完成按钮")
    @PutMapping("/{id}/done")
    public ApiResp done(@PathVariable Long id) {
        return sysFxOrderEntityService.done(id);
    }




}
